package com.wq.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wq.Result;
import com.wq.dto.DepartmentDTO;
import com.wq.entity.Department;
import com.wq.service.DepartmentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wq
 * @since 2025-11-09
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    /**
     * 根据ID查询部门信息（内部调用）
     */
    @GetMapping("/{id}")
    public Result<Department> getDepartmentById(@PathVariable Integer id) {
        Department department = departmentService.getById(id);
        return Result.success(department);
    }

    /**
     * 分页查询部门列表
     */
    @GetMapping("/list")
    public Result<Page<Department>> getDepartmentList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword) {
        Page<Department> page = departmentService.getDepartmentList(pageNum, pageSize, keyword);
        return Result.success(page);
    }

    /**
     * 获取所有部门（用于下拉框）
     */
    @GetMapping("/all")
    public Result<List<Department>> getAllDepartments() {
        List<Department> list = departmentService.getAllDepartments();
        return Result.success(list);
    }

    /**
     * 添加部门
     */
    @PostMapping("/add")
    public Result<Department> addDepartment(@RequestBody DepartmentDTO departmentDTO) {
        Department department = departmentService.addDepartment(departmentDTO);
        return Result.success(department);
    }

    /**
     * 更新部门
     */
    @PutMapping("/update/{id}")
    public Result<Boolean> updateDepartment(@PathVariable Integer id, @RequestBody DepartmentDTO departmentDTO) {
        Boolean result = departmentService.updateDepartment(id, departmentDTO);
        return Result.success(result);
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteDepartment(@PathVariable Integer id) {
        Boolean result = departmentService.deleteDepartment(id);
        return Result.success(result);
    }
}
